package com.zhangyu.shadow.repair.delegate.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.delegate.dept.DeptDelegate;
import com.zhangyu.shadow.repair.event.CheckVersionEvent;
import com.zhangyu.shadow.repair.event.LogoutEvent;
import com.zhangyu.shadow.repair.manager.AccountManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import st.lib.base.delegate.ShadowDelegate;
import st.lib.base.util.utilcode.util.AppUtils;

/**
 * @author zhangyu
 * @date 2018/3/28
 */

public class SettingDelegate extends ShadowDelegate {

    public static SettingDelegate newInstance() {
        return new SettingDelegate();
    }

    @BindView(R2.id.tv_name)
    TextView mTvName;
    @BindView(R2.id.tv_factory)
    TextView mTvFactory;
    @BindView(R2.id.tv_group)
    TextView mTvGroup;
    @BindView(R2.id.tv_version)
    TextView mTvVersion;
    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.tv_account)
    TextView mTvCount;
    @BindView(R2.id.tv_version_history)
    TextView mTvHistory;
    @BindView(R2.id.tv_help)
    TextView mTvHelp;

    @Override
    public Object setLayout() {
        return R.layout.delegate_setting;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setTitle(getString(R.string._setting));
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        init();
    }

    private void init() {
        if (getContext() == null) {
            return;
        }
        VectorDrawableCompat drawableCompat = VectorDrawableCompat.create(getContext()
                        .getResources(),
                R.drawable.ic_chevron_right_black_24dp, getContext().getTheme());
        mTvCount.setText(AccountManager.getInstance().getWorkCode());
        mTvFactory.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableCompat, null);
        mTvFactory.setText(AccountManager.getInstance().getDeptName());
        mTvGroup.setText(AccountManager.getInstance().getGroupName());
        mTvGroup.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableCompat, null);
        mTvVersion.setText(AppUtils.getAppVersionName());
        mTvVersion.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableCompat, null);
        mTvName.setText(AccountManager.getInstance().getWorkName());
        mTvHistory.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableCompat, null);
        mTvHelp.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableCompat, null);
    }

    @OnClick(R2.id.layout_version_history)
    public void onVersionHistory() {
        start(VersionDelegate.newInstance());
    }

    @OnClick(R2.id.layout_help)
    public void onHelp() {
        start(HelpDelegate.newInstance());
    }

    @OnClick(R2.id.btn_logout)
    public void onLogout() {
        EventBus.getDefault().post(new LogoutEvent());
    }


    @OnClick({R2.id.layout_dept, R2.id.layout_group})
    public void onModifyOrg() {
        if (!isFastClick()) {
            start(DeptDelegate.newInstance(true));
        }
    }

    @Override
    public boolean onBackPressedSupport() {
        pop();
        return true;
    }

    @OnClick(R2.id.layout_version)
    public void onCheckVersion() {
        EventBus.getDefault().post(new CheckVersionEvent());
    }
}
